package com.example.children.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.children.DTO.AttendanceTeacherDTO;
import com.example.children.DTO.BehaviorTeacherDTO;
import com.example.children.Model.Attendance;
import com.example.children.Service.BehaviorService;
import com.example.children.Util.Tools;
import com.example.children.Util.UUIDGenerator;
import com.example.children.Util.tool;
import com.example.children.Model.Behavior;
import com.example.children.Model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Behaviors")
public class BehaviorController {

    @Autowired
    private BehaviorService behaviorService;


    // 分页所有日常记录
    @GetMapping("/All")
    public ResponseModel<Page<BehaviorTeacherDTO>> getAttendanceByStudent(
            @RequestParam(defaultValue = "1") int pageNo,  // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        System.out.println("进入查询日常All方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        Page<BehaviorTeacherDTO> behaviorTeacherDTO=behaviorService.GetAllBehavior(pageNo,pageSize);
        if(behaviorTeacherDTO == null || behaviorTeacherDTO.getRecords().isEmpty()){
            System.out.println("没有日常数据");
            msg= tool.fail;
        }
        return new ResponseModel<>(behaviorTeacherDTO,code,msg,null);//响应给前端的对象
    }

    // 根据学生ID获取行为记录
    @GetMapping("/AllBehavior")
    public ResponseModel<List<Behavior>> getBehaviorByStudentId(@RequestParam String studentId) {
        System.out.println("进入查询getBehaviorByStudentId方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Behavior> behaviors=behaviorService.getBehaviorByStudent(studentId);
        if(behaviors==null||behaviors== new ArrayList<Behavior>()||behaviors.isEmpty()){
            msg= tool.fail;
        }
        return new ResponseModel<>(behaviors,code,msg,null);//响应给前端的对象
    }

    // 获取单个行为记录
    @GetMapping("/getBehavior")
    public ResponseModel<Behavior> getBehavior(@RequestParam String behaviorId) {
        System.out.println("进入getBehavior方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
         Behavior behaviors=behaviorService.getById(behaviorId);
        if(behaviors==null){
            msg= tool.fail;
        }
        return new ResponseModel<>(behaviors,code,msg,null);//响应给前端的对象
    }

    // 添加行为记录
    @PostMapping("/addBehavior")
    public ResponseModel<Behavior> addBehavior(@RequestBody Behavior behavior) {
        System.out.println("进入addBehavior方法：");
        String uuid = UUIDGenerator.generateNumericUUID(8);
        behavior.setBehaviorId(uuid);
        Integer code= Tools.Success;
        String msg= tool.success;
        behaviorService.save(behavior);
        return new ResponseModel<>(behavior,code,msg,null);//响应给前端的对象
    }

    // 更新行为记录
    @PostMapping("/updateBehavior")
    public ResponseModel<Behavior> updateBehavior(@RequestBody Behavior behavior) {
        System.out.println("进入updateBehavior方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!behaviorService.updateById(behavior)){
            msg= tool.fail;
        }
        return new ResponseModel<>(behavior,code,msg,null);//响应给前端的对象
    }

    // 删除行为记录
    @DeleteMapping("/deleteBehavior")
    public ResponseModel<String> deleteBehavior(@RequestParam String behaviorId) {
        System.out.println("进入deleteBehavior方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!behaviorService.removeById(behaviorId)){
            msg= tool.fail;
        }
        return new ResponseModel<>(behaviorId,code,msg,null);// 根据ID删除行为记录
    }


    // 日期搜索日常行为记录
    @PostMapping("/selectByDateBehaviorId")
    public ResponseModel<List<Behavior>> selectByDateBehaviorId(@RequestBody Behavior behavior) {
        System.out.println("进入selectByDateBehaviorId方法："+behavior.getDate()+"  "+behavior.getStudentId());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Behavior> behaviors=behaviorService.selectByDateBehaviorId(behavior);
        if(behaviors == null || behaviors.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(behaviors, code, msg, null); // 成功响应
    }

    // 搜索记录
    @PostMapping("/AdminSelectByDateBehaviorId")
    public ResponseModel<List<BehaviorTeacherDTO>> AdminSelectByDateBehaviorId(@RequestBody BehaviorTeacherDTO behaviorTeacherDTO) {
        System.out.println("进入AdminSelectByDateBehaviorId方法："+behaviorTeacherDTO.getDate()+"  "+behaviorTeacherDTO.getStudentId());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<BehaviorTeacherDTO> attendances=behaviorService.AdminselectByDateSend(behaviorTeacherDTO);
        if(attendances == null || attendances.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(attendances, code, msg, null); // 成功响应
    }

}

